﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimpleDatabaseAdapter
{
    internal class TableCreator
    {
        private string AllDataTableName
        {
            get
            {
                return _Parent.Config.TableAllDataName;
            }
        }
        private string ListsTableName
        {
            get
            {
                return _Parent.Config.TableListsName;
            }
        }

        SimpleDataAdapter _Parent;
        internal TableCreator(SimpleDataAdapter parent)
        {
            _Parent = parent;
        }

        internal void CheckAllDataTable()
        {
            string queryCheckExistsTable = GetSQLQueryAllDataCheckExistsTable();
            _Parent.DatabaseAccess.ExecuteQuery(queryCheckExistsTable);

            List<string> queryesCreateColumns = GetQueryesAllDataCreateColumns();
            foreach (string query in queryesCreateColumns)
            {
                try
                {
                    _Parent.DatabaseAccess.ExecuteQuery(query);
                }
                catch { }
            }
        }
        private string GetSQLQueryAllDataCheckExistsTable()
        {
            string queryCreateTable = string.Format(SQLPatterns.SQL_QUERY_ALL_DATA_CREATE_TABEL_PATTERN, AllDataTableName, AllDataColumns.COLUMN_ID);
            string queryCheckExistsTable = string.Format(SQLPatterns.SQL_QUERY_CHECK_EXISTS_TABEL_PATTERN, AllDataTableName, queryCreateTable);
            return queryCheckExistsTable;
        }
        private List<string> GetQueryesAllDataCreateColumns()
        {
            string addColumnPattern = string.Format(SQLPatterns.SQL_QUERY_ALL_DATA_ALERT_TABLE_PATTERN, AllDataTableName);
            string columnNvarchar255Pattern = string.Format(SQLPatterns.SQL_QUERY_ALL_DATA_COLUMN_NVARCHAR_255_PATTERN, AllDataColumns.columnNvarchar255Name);
            string columnNvarcharMAXPattern = string.Format(SQLPatterns.SQL_QUERY_ALL_DATA_COLUMN_NVARCHAR_MAX_PATTERN, AllDataColumns.columnNvarcharMAXName);
            string columnNvarcharIntPattern = string.Format(SQLPatterns.SQL_QUERY_ALL_DATA_COLUMN_INT_PATTERN, AllDataColumns.columnNvarcharIntName);
            string columnNvarcharFloatPattern = string.Format(SQLPatterns.SQL_QUERY_ALL_DATA_COLUMN_FLOAT_PATTERN, AllDataColumns.columnNvarcharFloatName);
            string columnNvarcharBitPattern = string.Format(SQLPatterns.SQL_QUERY_ALL_DATA_COLUMN_BIT_PATTERN, AllDataColumns.columnNvarcharBitName);
            string columnNvarcharDatetimePattern = string.Format(SQLPatterns.SQL_QUERY_ALL_DATA_COLUMN_DATETIME_PATTERN, AllDataColumns.columnNvarcharDatetimeName);

            List<string> queryes = new List<string>();

            queryes.Add(string.Format(SQLPatterns.SQL_QUERY_ALL_DATA_CREATE_COLUMN_CREATE_DATE_PATTERN, AllDataTableName, AllDataColumns.COLUMN_CREATE_DATA));
            queryes.Add(string.Format(SQLPatterns.SQL_QUERY_ALL_DATA_CREATE_COLUMN_MODIFY_NAME_PATTERN, AllDataTableName, AllDataColumns.COLUMN_MODIFY_DATA));
            queryes.Add(string.Format(SQLPatterns.SQL_QUERY_ALL_DATA_CREATE_COLUMN_LIST_ID_PATTERN, AllDataTableName, AllDataColumns.COLUMN_LIST_ID));

            queryes.AddRange(GetColumnQueryAllData(addColumnPattern, columnNvarchar255Pattern, AllDataColumns.columnNvarchar255Count));
            queryes.AddRange(GetColumnQueryAllData(addColumnPattern, columnNvarcharMAXPattern, AllDataColumns.columnNvarcharMAXCount));
            queryes.AddRange(GetColumnQueryAllData(addColumnPattern, columnNvarcharIntPattern, AllDataColumns.columnNvarcharIntCount));
            queryes.AddRange(GetColumnQueryAllData(addColumnPattern, columnNvarcharFloatPattern, AllDataColumns.columnNvarcharFloatCount));
            queryes.AddRange(GetColumnQueryAllData(addColumnPattern, columnNvarcharBitPattern, AllDataColumns.columnNvarcharBitCount));
            queryes.AddRange(GetColumnQueryAllData(addColumnPattern, columnNvarcharDatetimePattern, AllDataColumns.columnNvarcharDatetimeCount));

            return queryes;
        }
        private List<string> GetColumnQueryAllData(string addColumnPattern, string columnPattern, int count)
        {
            List<string> queryes = new List<string>();
            string column;

            for (int i = 0; i < count; i++)
            {
                column = string.Format(columnPattern, i);
                queryes.Add(string.Format(addColumnPattern, column));
            }

            return queryes;
        }

        internal void CheckListsTable()
        {
            string queryCheckExistsTable = GetSQLQueryListsCheckExistsTable();
            _Parent.DatabaseAccess.ExecuteQuery(queryCheckExistsTable);

            List<string> queryesCreateColumns = GetQueryesListsCreateColumns();
            foreach (string query in queryesCreateColumns)
            {
                try
                {
                    _Parent.DatabaseAccess.ExecuteQuery(query);
                }
                catch { }
            }
        }
        private string GetSQLQueryListsCheckExistsTable()
        {
            string queryCreateTable = string.Format(SQLPatterns.SQL_QUERY_LISTS_CREATE_TABEL_PATTERN, ListsTableName, ListColumns.COLUMN_ID);
            string queryCheckExistsTable = string.Format(SQLPatterns.SQL_QUERY_CHECK_EXISTS_TABEL_PATTERN, ListsTableName, queryCreateTable);
            return queryCheckExistsTable;
        }
        private List<string> GetQueryesListsCreateColumns()
        {
            List<string> queryesCreateColumns = new List<string>();
            queryesCreateColumns.Add(string.Format(SQLPatterns.SQL_QUERY_LISTS_CREATE_COLUMN_TITLE_PATTERN, ListsTableName, ListColumns.COLUMN_TITLE));
            queryesCreateColumns.Add(string.Format(SQLPatterns.SQL_QUERY_LISTS_CREATE_COLUMN_STATIC_NAME_PATTERN, ListsTableName, ListColumns.COLUMN_STATIC_NAME));
            queryesCreateColumns.Add(string.Format(SQLPatterns.SQL_QUERY_LISTS_CREATE_COLUMN_COLUMNS_PATTERN, ListsTableName, ListColumns.COLUMN_COLUMNS));
            return queryesCreateColumns;
        }
    }
}
